package com.jstyle.beatecg.adapter;

import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jstyle.beatecg.R;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/1/5.
 */

public class EcgReportAdapter extends RecyclerView.Adapter {
    String[] ecgArrays;
    String[] ecgArrays2;
    int[] analyze;
    private final static int View_Type_Normal = 2;
    private final static int View_Type_AF = 1;

    public EcgReportAdapter(String[] ecgArrays, String[]ecgArrays2,int[] analyze) {
        this.ecgArrays = ecgArrays;
        this.ecgArrays2 = ecgArrays2;
        this.analyze = analyze;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case View_Type_AF:
                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_ecg_report, parent, false);
                return new EcgReportViewHolder(view);
            case View_Type_Normal:
                View normal = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_ecg_report2, parent, false);
                return new EcgReportViewHolder2(normal);
            default:
                View normal2 = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_ecg_report2, parent, false);
                return new EcgReportViewHolder2(normal2);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
       switch (getItemViewType(position)){
           case View_Type_AF:
               bindAf(holder,position);
               break;
           case View_Type_Normal:
               bindNormal(holder,position-1);
                   break;
       }

    }

    private void bindNormal(RecyclerView.ViewHolder holder,int position){
        EcgReportViewHolder2 viewHolder = (EcgReportViewHolder2) holder;
        String content = ecgArrays[position*4];
        String contentRight = ecgArrays2[position*4];
        int data = analyze[position*4];
        viewHolder.textView2.setText(content);
        viewHolder.textView2_right.setText(contentRight);
        viewHolder.rl1.setSelected(data!=0);

        String content2 = ecgArrays[position*4+1];
        String contentRight2 = ecgArrays2[position*4+1];
        int data2 = analyze[position*4+1];
        viewHolder.textView3.setText(content2);
        viewHolder.textView3_right.setText(contentRight2);
        viewHolder.rl2.setSelected(data2!=0);

        String content3 = ecgArrays[position*4+2];
        String contentRight3 = ecgArrays2[position*4+2];
        int data3 = analyze[position*4+2];
        viewHolder.textView4.setText(content3);
        viewHolder.textView4_right.setText(contentRight3);
        viewHolder.rl3.setSelected(data3!=0);

        String content4 = ecgArrays[position*4+3];
        String contentRight4 = ecgArrays2[position*4+3];
        int data4 = analyze[position*4+3];
        viewHolder.textView5.setText(content4+"\n"+contentRight4);
        viewHolder.textView5.setSelected(data4!=0);
    }

    private void bindAf(RecyclerView.ViewHolder holder,int position){
        EcgReportViewHolder viewHolder = (EcgReportViewHolder) holder;
        String content = ecgArrays[position];
        int data = analyze[position];
//        viewHolder.sportType.setSelected(data!=0);
//        viewHolder.sportType.setText(content);
    }
    @Override
    public int getItemViewType(int position) {
        int viewType = 0;
        if (position == 0) {
            viewType = View_Type_AF;
        } else {
            viewType = View_Type_Normal;
        }
        return viewType;
    }

    @Override
    public int getItemCount() {
        return analyze == null ? 0 : 4;
    }

    public class EcgReportViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.textView_item_ecg)
        Button sportType;

        public EcgReportViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public class EcgReportViewHolder2 extends RecyclerView.ViewHolder {
        @BindView(R.id.rl_1)
        RelativeLayout rl1;
        @BindView(R.id.rl_2)
        RelativeLayout rl2;
        @BindView(R.id.rl_3)
        RelativeLayout rl3;
        @BindView(R.id.textView2)
        TextView textView2;
        @BindView(R.id.textView2_right)
        TextView textView2_right;
        @BindView(R.id.textView3_right)
        TextView textView3_right;
        @BindView(R.id.textView4_right)
        TextView textView4_right;
        @BindView(R.id.textView3)
        TextView textView3;
        @BindView(R.id.textView4)
        TextView textView4;
        @BindView(R.id.textView5)
        TextView textView5;

        public EcgReportViewHolder2(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


}
